package com.onefinger.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.onefinger.aspect.Cache;
import com.onefinger.aspect.Uncache;
import com.onefinger.domain.Item;
import com.onefinger.repository.GenericRepository;

public class ItemService implements IItemService {

	@Autowired
	GenericRepository<Item> genericRepository; 

	@Autowired
	ILanguageService languageService;

	@Cache(cacheKey="items", expireTime=0)
	public List<Item> list() {
		return genericRepository.getList(Item.class);
	}

	
	@Uncache(cacheKey="items")
	public boolean save(Item item) {
		try {
			genericRepository.save(item);
			languageService.updateAllVersions();
		} catch (Exception e) {
			
			return false;
		}
		return true;
	}

	@Uncache(cacheKey="items")
	public boolean remove(Item item) {
		try {
			genericRepository.remove(item);
			languageService.updateAllVersions();
		} catch (Exception e) {
			
			return false;
		}
		return true;
	}


	@Override
	public Item findById(Long itemId) {
		return genericRepository.find(Item.class, itemId);
	}

}
